var DATA_UPLOADS_CMS_URL = DOMAIN+'/uploads/';
Media = {
	defaults : {
		keyword : null,
		filetype : null,
		folder: null,
		page : 1,
		rpp  : 24,
		callback : function(){ alert('No function to execute!'); }
	},
	show : function(input){
		//merge  input with default properties
		if( typeof(input) === 'undefined' ){
			input = this.defaults;
		}
		else{
			for(var p in this.defaults){
				if( typeof(input[p]) === 'undefined' ) input[p] = this.defaults[p];
			}
		}
		this.defaults = input;
		//show popup
		$.fancybox.showLoading();
		$.ajax({
			async:false,
			type: "POST",
			url: "/admincp/media/popup"
		}).done(function( html ) {
			$.fancybox(html);
		});
		this.load();
	},
	load : function(page){
		if(typeof(page) !== 'undefined'){
			this.defaults.page = page;
		}
		//get data via ajax
		var data = '';
		$.ajax({
			async: false,
			type: "POST",
			dataType: 'json',
			url: "/admincp/media/index",
			data: { page: this.defaults.page, rpp: this.defaults.rpp, keyword: this.defaults.keyword, filetype: this.defaults.filetype, folder: this.defaults.folder },
			async: false
		}).done(function(json) {
			data = json;
		});

		//generate html
		$('#media-list').html(this.generateHTML(data));
		$('#media-pagination').html(this.generatePagination(data));
		
		//add event
		var src = '';
		var title = '';
		var desc = '';
		var ext = '';
		
		$('#media-list li').click(function(){
			/*focus*/
			$('#media-list li').removeClass('clicked');
			$(this).addClass('clicked');
	
			/*insert properties*/
			src = $(this).children('div').attr('media-file');
			title = $(this).children('div').attr('media-title');
			desc = $(this).children('div').attr('media-description');
			ext = $(this).children('div').attr('media-ext');

			$('#f_url').val(src);
			$('#f_title').val(title);
			$('#f_description').val(desc);
			$('#media-popup #media-sidebar .thumbnail img').attr('src', DATA_UPLOADS_CMS_URL+src);
	
			if(jQuery.inArray(ext, [ 'jpg', 'jpeg', 'png', 'gif' ]) > -1){
				$('#f_thumbs').show();
			}else{
				$('#f_thumbs').hide();
			};
		});

		$('#mFiletype').off('change').change(function(){
			Media.defaults.filetype = $(this).val();
			Media.load();
			//this.defaults.filetype = this.value;
			//this.load();
		});
		
		$('#mSearch').off('click').click(function(){
			Media.defaults.keyword = $('#mKeyword').val();
			Media.load();
			//this.defaults.filetype = this.value;
			//this.load();
		});

		var callback = this.defaults.callback;
		$('#b_insert').off('click').click(function(){
			if (typeof(callback) === "function"){
				callback(src);
			} else {
				alert('callback must be a function');
			}
		});
		
		//set cookie
		//cVal = '({"page": "'+page+'", "filter": "'+filter+'", "keyword": "'+keyword+'"})';
		//setCookie(cName, cVal);
		
	},
	generateHTML : function(data){
		var jdata = data['list'];
		var html = '';

		if(typeof(jdata) === 'undefined') return html;
		
		for(var i = 0; i<jdata.length; i++){
			var mfile = jdata[i].media_file.replace(/\\/g,'/');
			var mtitle = jdata[i].media_title;
			var mdesc = jdata[i].media_description;
			var mext = jdata[i].media_file_extension;
			
			var isImg = jQuery.inArray(mext, [ 'jpg', 'jpeg', 'png', 'gif' ]);
			var isFlash = jQuery.inArray(mext, [ 'swf' ]);
			var isVideo = jQuery.inArray(mext, [ 'flv', 'mp4', 'ogg', 'ogv', 'avi', 'mpeg', 'mov', 'wmv', 'mkv', '3gp' ]);
			var mpreview = '';
	
			if(isImg>-1) {
				var mthumb = mfile.replace('src/', 'thumbs/120x120/');
				mpreview = '<img class="lazy" src="'+DATA_UPLOADS_CMS_URL+mthumb+'" style="max-height:75px; max-width:75px;"/>';
			}
			else if(isFlash>-1) mpreview = ' <object width="75" height="75" data="'+DATA_UPLOADS_CMS_URL+mfile+'"></object>';
			else if(isVideo>-1) mpreview = '<img class="lazy" src="img/video.png" width="75px" height="75px" />';
			else mpreview = '<img class="lazy" src="img/file.png" width="75px" height="75px" />';
			
			html += '<li><div media-file="'+mfile+'" media-tile="'+mtitle+'" media-description="'+mdesc+'" media-ext="'+mext+'">';
			html += '	<div class="preview">'+mpreview+'</div>';
			html += '	<div class="title">'+mtitle+'</div>';
			html += '</div></li>';
		}
		
		if(jdata.length == 0 || html == ''){
			html = ('<div class="message">No data found!<br>'+data['message']+'</div>');		
		}
		
		return html;
	},
	generatePagination : function(data){
		var jdata = data['pagination'];
		var html = '';
		
		if(typeof(jdata) === 'undefined') return html;
		
		var beginPageRange = Math.max(0, jdata.currentPage-Math.ceil(jdata.pageRange/2));
		var endPageRange = beginPageRange+jdata.pageRange-1;
		if(endPageRange >= jdata.pageCount)
		{
			endPageRange=jdata.pageCount-1;
			beginPageRange=Math.max(0, endPageRange-jdata.pageRange+1);
		}
		
		if(jdata.beginPageRange > 1){
			html += '<li><a href="javascript:void(0);" onclick="Media.load(1)">1</a></li><li>...</li>';
		}
		for (var i = beginPageRange; i <= endPageRange; i++){
			if(jdata.currentPage == i){
				html += '<li class="current">'+(i+1)+'</li>';
			}
			else{
				html += ' <li><a href="javascript:void(0);" onclick="Media.load('+(i+1)+')">'+(i+1)+'</a></li>';
			}
		}
		if(jdata.endPageRange < jdata.pageCount){
			html += '<li>...</li><li><a href="javascript:void(0);" onclick="Media.load('+jdata.pageCount+')">'+jdata.pageCount+'</a></li>';
		}
		return html;
	},
	uploadFile : function(uploadUrl, fileElementId){
		$.ajaxFileUpload({
			url: uploadUrl,
			secureuri:false,
			type: "POST",
			fileElementId: fileElementId,
			data: {folder: this.defaults.folder},
			beforeSend : function(){},
			success : function(data)
			{ 
				/*if(data != '-1' && data != '0'){
					if (typeof(func) == "function"){
						func(IMAGES_TN+'/'+data);
					} 
					else{
						$('a[href="#tab-MyImage"]').trigger('click');
						process(1, func);
					}
				}
				else window.location.reload();*/
				$('#url').val('');
				$('#media').val('');
				Media.load(1);
				$( "#media-popup" ).tabs({active : 0});
			}
		});
	},
	download : function(downloadUrl, url){
		$.ajax({
			url: downloadUrl,
			type: "POST",
			data: {url: url, folder: this.defaults.folder}
		}).done(function(data) {
			$('#url').val('');
			$('#media').val('');
			Media.load(1);
			$( "#media-popup" ).tabs({active : 0});
		});	
	}
}